﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO;
using EntityFramework;
using System.Data;


namespace m.Website.Controls.Touch
{
    public partial class BoxProduct : System.Web.UI.UserControl
    {
        public int ZoneFocus { get; set; }
        public string InnerClass { get; set; }

        private const string ProductFocusItemFormat = @"<div class=""wrap-item"">
<div class=""item {0} loadswipe"" >  
                    <div class=""img"">
                        <a href=""{4}"" title=""{3}""><img title=""{10}"" alt=""{3}"" src=""{5}"" /></a>
                    </div>
                    <div class=""content-right"">{1}
                        <div class=""name""><a href=""{4}"" title=""{3}"">{2}</a></div>
                        <div class=""code-name"">{6}</div>
                        <div class=""desc"">{7}</div>
                        <div class=""shopping"">
                            <a href=""javascript:void(0);"" class=""cart""></a>
                            <div class=""price"">
                                <div class=""current"">{8}</div>
                                <div class=""discount"">{9}</div>
                            </div>
                            <div class=""clear""></div>
                        </div>
                    </div>
                </div>
</div>";
        private const string ProductNewFormat = "<i class=\"new\"></i>";

        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = BO.ProductUserBO.ProductUBO.GetTopHomeProductBO("5", ZoneFocus.ToString());
            if (dt.Rows.Count > 0)
            {
                DataTable dtZone = BO.ProductUserBO.ProductUBO.GetInfomationZoneBO(ZoneFocus.ToString());
                if (dtZone.Rows.Count > 0)
                {
                    ltrFocus.Text = "<a href=\"/" + dtZone.Rows[0][Const.tbZoneProduct.url].ToString() + ".htm\">" + dtZone.Rows[0][Const.tbZoneProduct.title].ToString() + "</a>";
                }
                var sb = new StringBuilder();
                var count = 1;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string cls = (i + 1) % 3 == 0 ? "mr0" : string.Empty;
                    long date = 0;
                    long.TryParse(dt.Rows[i][Const.tbProducts.EndDateNew].ToString(), out date);
                    var pNew = date > 0 && date >= long.Parse(DateTime.Now.ToString("yyyyMMdd")) ? ProductNewFormat : string.Empty;

                    var price = int.Parse(dt.Rows[i][Const.tbProducts.TypeSale].ToString()) == 1 ? "Giá: Liên hệ" : Utils.ConvertMoneyNice(dt.Rows[i][Const.tbProducts.Price].ToString(), BO.Const.FormatMoney);
                    var sale = int.Parse(dt.Rows[i][Const.tbProducts.TypeSale].ToString()) == 1 ? string.Empty : Utils.ConvertMoneyNice(dt.Rows[i][Const.tbProducts.Sale].ToString(), BO.Const.FormatMoney);
                    var sapo = dt.Rows[i][Const.tbProducts.Sapo].ToString();
                    if (int.Parse(dt.Rows[i][Const.tbProducts.Sale].ToString()) == 0)
                    {
                        sale = "";
                    }
                    string sTitle = dt.Rows[i][Const.tbProducts.Title].ToString();
                    sb.AppendFormat(ProductFocusItemFormat, cls
                        , pNew
                        , sTitle
                        , HttpUtility.HtmlEncode(sTitle)
                        , dt.Rows[i][Const.tbProducts.Url].ToString()
                        , BO.Utils.CropImageMobile(150, 150, BO.Utils.GetOneImagesByArr(dt.Rows[i][Const.tbProducts.Avatar].ToString()))
                        , "Mã sản phẩm: " + dt.Rows[i][Const.tbProducts.ProductCode].ToString()
                        , ""//dt.Rows[i][Const.tbProducts.Sapo].ToString()
                        , price
                        , sale
                        , sapo);
                    count++;
                }
                ltrListFocus.Text = sb.ToString();
            }
            else
                this.Visible = false;
        }
    }
}